package com.example.weblending.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weblending.domain.ApplyUnset;
import com.example.weblending.domain.User;
import com.example.weblending.mapper.UserMapper;
import com.example.weblending.service.ApplyUnsetService;
import com.example.weblending.mapper.ApplyUnsetMapper;
import com.example.weblending.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class ApplyUnsetServiceImpl extends ServiceImpl<ApplyUnsetMapper, ApplyUnset>
implements ApplyUnsetService{

    @Resource
    private UserMapper userMapper;

    @Autowired
    private MailUtil mailUtil;

    @Override
    public void disable(int uid) {
        QueryWrapper<ApplyUnset> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",uid);
        wrapper.eq("state",1);
        List<ApplyUnset> list=this.list(wrapper);
        list.forEach((i)->{
            i.setState(-1);
            this.updateById(i);
        });
    }

    /**
     * 提交密码验证申请
     * @param applyUnset
     * @return
     */
    @Override
    public boolean apply(ApplyUnset applyUnset) {
        applyUnset.setApplyTime(new Timestamp(System.currentTimeMillis()));
        applyUnset.setState(0);
        return this.save(applyUnset);
    }

    @Override
    public Page<ApplyUnset> getListyId(long page, long limit, User user) {
        QueryWrapper<ApplyUnset> wrapper = new QueryWrapper<>();
        wrapper.eq("state",0);
        wrapper.eq("premission",user.getPermission()-1);
        wrapper.orderByAsc("apply_time");
        List<String> noSelected = new ArrayList<>();
        Page<ApplyUnset> pageParam = new Page<>(page,limit);
        baseMapper.selectPage(pageParam,wrapper);
        List<ApplyUnset> list=pageParam.getRecords();
        for (int i = 0; i < list.size(); i++) {
            ApplyUnset temp=list.get(i);
            User applyUser=userMapper.selectById(temp.getUid());
            temp.setName(applyUser.getName());
            temp.setEmail(applyUser.getEmail());
            temp.setUserStatue(applyUser.getStatus());
            temp.setCertificate(null);
        }
        //map返回
        return pageParam;
    }

    @Override
    public Page<ApplyUnset> getHistoryId(long page, long limit, User user) {
        QueryWrapper<ApplyUnset> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.eq("premission",user.getPermission()-1);
        wrapper.orderByDesc("deal_time");
        List<String> noSelected = new ArrayList<>();
        // 进行select排除列判断
        if (noSelected.size()>0) {
            wrapper.select(item -> !noSelected.contains(item.getColumn()));
        }
        Page<ApplyUnset> pageParam = new Page<>(page,limit);
        baseMapper.selectPage(pageParam,wrapper);
        List<ApplyUnset> list=pageParam.getRecords();
        for (int i = 0; i < list.size(); i++) {
            ApplyUnset temp=list.get(i);
            User applyUser=userMapper.selectById(temp.getUid());
            temp.setName(applyUser.getName());
            temp.setEmail(applyUser.getEmail());
            temp.setUserStatue(applyUser.getStatus());
            temp.setCertificate(null);
        }
        //map返回
        return pageParam;
    }

    @Override
    public int getNum(User user,boolean finish) {
        QueryWrapper<ApplyUnset> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        if (finish){
            wrapper.ne("state",0);
        }else{
            wrapper.eq("state",0);
        }
        wrapper.eq("premission",user.getPermission()-1);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public boolean reset(ApplyUnset applyUnset, User operator) {
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        applyUnset.setCode(uuid);
        applyUnset.setDealTime(new Timestamp(System.currentTimeMillis()));
        applyUnset.setState(1);
        applyUnset.setOid(operator.getUid());
        boolean success=this.updateById(applyUnset);
        User user=userMapper.selectById(applyUnset.getUid());
        user.setUnset(1);
        success&=userMapper.updateById(user)!=-1;
        mailUtil.getOneCode(user.getName(),uuid,user.getEmail());
        return success;
    }
}




